package dshop.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;

import appbase.third.xinge.XingePushKit;
import dshop.model.push.UserPushAccount;

public class XingePushService {

	public static void onPushProcurement(String content, int procurementId, List<Integer> userIdList)
	{
		String pustContent = "及时通推送采购信息：" + content + "";
		pushProcurementInfo(pustContent, procurementId, userIdList);
	}
	
	public static void onEndProcurement(String content, int procurementId, List<Integer> userIdList)
	{
		String pustContent = "及时通提醒采购信息已结束：" + content + "";
		pushProcurementInfo(pustContent, procurementId, userIdList);
	}
	
	
	private static void pushProcurementInfo(String content, int procurementId, List<Integer> userIdList)
	{
		List<UserPushAccount> accountList = UserPushAccount.findIn(userIdList);
		if (accountList == null || accountList.isEmpty())
		{
			return;
		}
		
		ArrayList<String> iosAccountList = new ArrayList<String>();
		ArrayList<String> androidAccountList = new ArrayList<String>();
		for (UserPushAccount userPushAccount : accountList) 
		{
			int platform = userPushAccount.getInt("platform");
			String pushAccount = userPushAccount.getStr("pushAccount");
			if (platform == UserPushAccount.PLATFORM_IOS)
			{
				iosAccountList.add(pushAccount);
			}else if (platform == UserPushAccount.PLATFORM_ANDROID)
			{
				androidAccountList.add(pushAccount);
			}
		}
		
		Map<String, Integer> customMap = new HashMap<String, Integer>();
		customMap.put("procurementId", procurementId);
		String customContent = JsonKit.toJson(customMap);
		
		if (!iosAccountList.isEmpty())
		{
			XingePushKit.pushNotifyAccountListIOS(content, customContent, iosAccountList);
		}
		if (!androidAccountList.isEmpty())
		{
			XingePushKit.pushNotifyAccountListAndroid(content, customContent, androidAccountList);
		}
	}

	
}
